package com.hinotoyk.testsystem.service;

import com.hinotoyk.testsystem.entity.*;
import com.hinotoyk.testsystem.mappers.StudentMapper;
import com.hinotoyk.testsystem.mappers.TeachStuMapper;
import com.hinotoyk.testsystem.mappers.TeacherMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @program: testsystem
 * @description:
 * @author: DYJ
 * @create: 2019-11-14 10:17
 **/

@Service
public class TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private TeachStuMapper teachStuMapper;

    @Resource
    private StudentService studentService;

    public Teacher getTeacher(String username) {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeacherNameEqualTo(username);
        List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
        return teachers.get(0);
    }

    public Teacher getTeacherById(int teacherId){
        return teacherMapper.selectByPrimaryKey(teacherId);
    }

    public int getTeacherId(String username){
        return getTeacher(username).getTeacherId();
    }

    public int register(Teacher teacher){
        return teacherMapper.insert(teacher);
    }


    public List<Teacher> getAllTeacher() {
        return teacherMapper.selectByExample(new TeacherExample());
    }

    //获取老师下面有多少学生
    public List<Student> getStudentByTeachId(int teacherId){
        TeachStuExample example = new TeachStuExample();
        example.createCriteria().andTeacherIdEqualTo(teacherId);
        List<TeachStu> teachStus = teachStuMapper.selectByExample(example);
        List<Student> list = new ArrayList<>();
        for(int i=0;i<teachStus.size();i++){
            Integer studentId = teachStus.get(i).getStudentId();
            Student student = studentService.getStudentById(studentId);
            list.add(student);
        }
        return list;
    }
}
    